package com.shuwen.gcdj.controller.basicsstructure;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shuwen.gcdj.bean.AddResp;
import com.shuwen.gcdj.bean.DictResp;
import com.shuwen.gcdj.bean.VoidResp;
import com.shuwen.gcdj.bean.structure.request.*;
import com.shuwen.gcdj.bean.structure.response.InspectionItemTemplateResp;
import com.shuwen.gcdj.bean.structure.response.StructurePlanResp;
import com.shuwen.gcdj.bean.structure.response.StructureTreeResp;
import com.shuwen.gcdj.bean.structure.response.ThirdStructurePageResp;
import com.shuwen.gcdj.common.annotation.JWTAuth;
import com.shuwen.gcdj.common.api.CommonPage;
import com.shuwen.gcdj.common.api.CommonResult;
import com.shuwen.gcdj.controller.AuthController;
import com.shuwen.gcdj.service.basicsstructure.BasicsStructureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.text.ParseException;
import java.util.List;

/**
 * @author :yys
 * 设备管理
 * @date : 2023/5/9 15:18
 */
@Api(tags = "基础运维 - 策略管理 - 设备")
@RestController
@RequestMapping("/structuremanagement")
@Slf4j
public class BasicsStructureController extends AuthController {

    @Autowired
    private BasicsStructureService basicsStructureService;


    @ApiOperation(value = "获取设备树形结构")
    @PostMapping(value = "/getstructuretree")
    @ResponseBody
    @JWTAuth
    public CommonResult<List<StructureTreeResp>> structureTree(@Valid @RequestBody StructureTreeReq req) {
        List<StructureTreeResp> structureTree = basicsStructureService.structureTree(req);
        return CommonResult.success(structureTree);
    }

    @ApiOperation(value = "获取一级结构绑定计划")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "一级结构id",required=true, dataType = "string")
    })
    @GetMapping(value = "/getplancycledetail/{id}")
    @ResponseBody
    @JWTAuth
    public CommonResult<StructurePlanResp> structureByPlan(@PathVariable("id") String id) {
        StructurePlanResp structurePlanResp = basicsStructureService.structureByPlan(id);
        return CommonResult.success(structurePlanResp);
    }


    @ApiOperation(value = "获取三级结构分页信息")
    @PostMapping(value = "/getthirdstructurepage")
    @ResponseBody
    @JWTAuth
    public CommonResult<CommonPage<ThirdStructurePageResp>> getThirdStructurePage(@Valid @RequestBody ThirdStructurePageReq req) {
        Page<ThirdStructurePageResp> tList = basicsStructureService.getThirdStructurePage(req);
        return CommonResult.success(CommonPage.restPage(tList));
    }

    @ApiOperation(value = "获取三级结构列表信息")
    @PostMapping(value = "/getThirdStructureList")
    @ResponseBody
    @JWTAuth
    public CommonResult<List<ThirdStructurePageResp>> getThirdStructureList(@Valid @RequestBody ThirdStructurePageReq req) {
        List<ThirdStructurePageResp> tList = basicsStructureService.getThirdStructure(req);
        return CommonResult.success(tList);
    }

    @ApiOperation(value = "添加一级设备结构")
    @PostMapping(value = "/addfirststructure")
    @ResponseBody
    @JWTAuth
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<AddResp> addFirstStructure(HttpServletRequest request, @RequestBody StructureFirstReq structureFirstReq) {
        AddResp addResp = basicsStructureService.addFirstStructure(initUser(request),structureFirstReq);
        return CommonResult.success(addResp);
    }

    @ApiOperation(value = "编辑一级设备结构（编辑计划）")
    @PostMapping(value = "/editfirststructure")
    @ResponseBody
    @JWTAuth
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<PlanParamsReq> editFirstStructure(HttpServletRequest request, @RequestBody StructureFirstReq structureFirstReq) {
        basicsStructureService.editFirstStructure(initUser(request),structureFirstReq);
        return CommonResult.success(structureFirstReq.getPlanParamsReq());
    }

    @ApiOperation(value = "批量删除一级设备结构")
    @PostMapping(value = "/batchdeletefirststructure")
    @ResponseBody
    @JWTAuth
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<VoidResp> batchFirstStructure(HttpServletRequest request,@RequestBody DeleteFirstStructure ids) {
        VoidResp voidResp = basicsStructureService.batchFirstStructure(initUser(request),ids);
        return CommonResult.success(voidResp);
    }

    @ApiOperation(value = "获取检查项模板信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id ", value = "二级结构id",required=true, dataType = "string")
    })
    @GetMapping(value = "/getinspectionitemtemplist")
    @ResponseBody
    @JWTAuth
    public CommonResult<List<InspectionItemTemplateResp>> getFirstStructure(@RequestParam String id) {
        List<InspectionItemTemplateResp> iList = basicsStructureService.getFirstStructure(id);
        return CommonResult.success(iList);
    }

    @ApiOperation(value = "添加二级设备结构")
    @PostMapping(value = "/addsecordstructure")
    @ResponseBody
    @JWTAuth
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<AddResp> addSeCordStructure(HttpServletRequest request, @RequestBody SeCordStructureReq seCordStructureReq) throws ParseException{
        AddResp addResp = basicsStructureService.addSeCordStructure(initUser(request),seCordStructureReq);
        return CommonResult.success(addResp);
    }

    @ApiOperation(value = "编辑二级设备结构（编辑三级设备）")
    @PostMapping(value = "/editsecordstructure")
    @ResponseBody
    @JWTAuth
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<VoidResp> editSeCordStructure(HttpServletRequest request, @RequestBody OperateReq req) throws ParseException {
        VoidResp voidResp = basicsStructureService.editSeCordStructure(initUser(request),req);
        return CommonResult.success(voidResp);
    }

    @ApiOperation(value = "批量删除二级设备结构")
    @PostMapping(value = "/batchsecordstructure")
    @ResponseBody
    @JWTAuth
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<VoidResp> batchSeCordStructure(HttpServletRequest request,@RequestBody DeleteFirstStructure ids) {
        VoidResp voidResp = basicsStructureService.batchSeCordStructure(initUser(request),ids);
        return CommonResult.success(voidResp);
    }



    @ApiOperation(value = "获取结构类型")
    @GetMapping(value = "/getstructuretypelist")
    @ResponseBody
    @JWTAuth
    public CommonResult<List<DictResp>> getstategytemplatelist() {
        List<DictResp> list = basicsStructureService.getstategytemplatelist();
        return CommonResult.success(list);
    }

    @ApiOperation(value = "获取一级部件结构配置信息")
    @GetMapping(value = "/getbasicsstructurefirstnamelist")
    @ResponseBody
    @JWTAuth
    public CommonResult<List<DictResp>> getbasicsstructurefirstnamelist() {
        List<DictResp> list = basicsStructureService.getbasicsstructurefirstnamelist();
        return CommonResult.success(list);
    }


    @ApiOperation(value = "获取二级部件结构配置信息")
    @GetMapping(value = "/getbasicsstructuresecondnamelist")
    @ResponseBody
    @JWTAuth
    public CommonResult<List<DictResp>> getbasicsstructuresecondnamelist() {
        List<DictResp> list = basicsStructureService.getbasicsstructuresecondnamelist();
        return CommonResult.success(list);
    }
}
